package com.jasonjan.jiance.module.base;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;


import com.afollestad.materialdialogs.color.CircleView;
import com.jasonjan.jiance.R;
import com.jasonjan.jiance.global.Constant;
import com.jasonjan.jiance.util.SettingUtil;
import com.r0adkll.slidr.Slidr;
import com.r0adkll.slidr.model.SlidrConfig;
import com.r0adkll.slidr.model.SlidrInterface;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import javax.annotation.Nullable;

/**
 * Created by JasonJan on 2017/11/30.
 */

public class BaseActivity extends RxAppCompatActivity {
    private static final String TAG="BaseActivity";
    protected SlidrInterface slidrInterface;
    private int iconType=-1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        //默认图标获取的是圆形circle
        this.iconType=SettingUtil.getInstance().getCustomIconValue();
        initSlidable();
    }

    @Override
    protected void onResume(){
        super.onResume();
        //获取主题色
        int color=SettingUtil.getInstance().getColor();
        //获取图标形状，圆，矩，正
        int drawable=Constant.ICONS_DRAWABLES[SettingUtil.getInstance().getCustomIconValue()];
        if(getSupportActionBar()!=null){
            //设置标题栏的颜色
            getSupportActionBar().setBackgroundDrawable(new ColorDrawable(color));
        }
        //如果SDK版本>=21，还要设置状态栏的颜色
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
            getWindow().setStatusBarColor(CircleView.shiftColorDown(color));
            //TaskDescription==>用于在最近的任务列表中设置和检索当前活动的信息
            ActivityManager.TaskDescription taskDescription=new ActivityManager.TaskDescription(
                    getString(R.string.app_name),
                    BitmapFactory.decodeResource(getResources(),drawable),
                    color
            );
            setTaskDescription(taskDescription);
            //setNavigationBar是底部导航栏，模拟器上会有
            if(SettingUtil.getInstance().getNavBar()){
                getWindow().setNavigationBarColor(CircleView.shiftColorDown(color));
            }else{
                getWindow().setNavigationBarColor(Color.BLACK);
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        if(item.getItemId()==android.R.id.home){
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed(){
        //Fragment逐个出栈
        int count=getSupportFragmentManager().getBackStackEntryCount();
        if(count==0){
            super.onBackPressed();
        }else{
            getSupportFragmentManager().popBackStack();
        }
    }

    @Override
    protected void onStop(){

        //如果iconType不为默认的圆形了
        if(iconType!=SettingUtil.getInstance().getCustomIconValue()){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //前提是在清单中已经完成配置别名
                    String act=".SplashActivity_";
                    for(String s:Constant.ICONS_TYPE){
                        getPackageManager().setComponentEnabledSetting(new ComponentName(BaseActivity.this,getPackageName()+act+s),
                                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                                PackageManager.DONT_KILL_APP);
                    }

                    act+=Constant.ICONS_TYPE[SettingUtil.getInstance().getCustomIconValue()];

                    getPackageManager().setComponentEnabledSetting(new ComponentName(BaseActivity.this,getPackageName()+act),
                            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                            PackageManager.DONT_KILL_APP);
                }
            }).start();
        }

        super.onStop();
    }

    /**
     * 初始化Toolbar
     */
    protected void initToolbar(Toolbar toolbar, boolean homeAsUpEnabled, String title){
        toolbar.setTitle(title);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(homeAsUpEnabled);
    }

    /**
     * 初始化滑动返回
     */
    protected void initSlidable(){
        int isSlidable=SettingUtil.getInstance().getSlidable();
        if(isSlidable!= Constant.SLIDABLE_DISABLE){
            SlidrConfig config=new SlidrConfig.Builder()
                                    .edge(isSlidable==Constant.SLIDABLE_EDGE)
                                    .build();
            slidrInterface= Slidr.attach(this,config);
        }
    }

}
